/* eslint no-extend-native: ["error", { "exceptions": ["Array"] }] */
(() => {
  /**
   * 克隆复制（简单copy而已）
   **/

  if (!Array.prototype.clone) {
    Array.prototype.clone = function() {
      return this.map((x) => {
        return JSON.parse(JSON.stringify(x));
      });
    };
  }
  /**
   * 清空
   **/
  if (!Array.prototype.clear) {
    Array.prototype.clear = function() {
      this.splice(0, this.length);
    };
  }
  /**
   * 数组包含元素
   **/

  if (!Array.prototype.contains) {
    Array.prototype.contains = function(el) {
      let i;
      for (i = 0; i < this.length; i++) {
        if (this[i] === el) return true;
      }
      return false;
    };
  }
  /**
   * 添加数组
   **/
  if (!Array.prototype.merge) {
    Array.prototype.merge = function(arr) {
      if (arr) {
        let i;
        for (i = 0; i < arr.length; i++) {
          this.push(arr[i]);
        }
      }
    };
  }

  /**
   * 根据值和属性获取到数组的对象下标
   **/
  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(val: any, field?: any) {
      let i = 0;
      for (; i < this.length; i++) {
        if (this[i] && (field ? this[i][field] === val : this[i] === val)) {
          return i;
        }
      }
      return -1;
    };
  }

  /**
   * 最后一个下标
   **/
  if (!Array.prototype.lastIndexOf) {
    Array.prototype.lastIndexOf = function(val: any, field?: any) {
      let i = 0;
      let max = -1;
      for (; i < this.length; i++) {
        if (this[i] && (field ? this[i][field] === val : this[i] === val)) {
          max = i;
        }
      }
      return max;
    };
  }

  /**
   * 简单去重，查找数组唯一
   **/
  if (!Array.prototype.unique) {
    Array.prototype.unique = function() {
      return [...new Set(this)];
    };
  }

  /**
   * 数组最大值
   **/
  if (!Array.prototype.max) {
    Array.prototype.max = function(field?: string | number) {
      let result = this[0];

      let i = 0;
      for (; i < this.length; i++) {
        const val = field ? this[i][field] : this[i];
        if (val > result) result = val;
      }

      return result;
    };
  }

  /**
   * 数组最小值
   **/
  if (!Array.prototype.min) {
    Array.prototype.min = function(field?: string | number) {
      let result = this[0];

      let i = 0;
      for (; i < this.length; i++) {
        const val = field ? this[i][field] : this[i];
        if (val < result) result = val;
      }

      return result;
    };
  }
})();
export {};
